import { Link } from '@brillout/docpress'

You can install error tracking:
 - On the client-side, by using the <Link href="/client">`+client.js`</Link> file.
 - On the server-side, by using the <Link href="/onError">`+onError()`</Link> hook.


## Client-side

You can use <Link href="/client">`+client.js`</Link> in order to make sure that your error tracker is initialized before any other code executes.

```ts
// pages/+client.ts

// This is a good place to initialize an error tracker such as Sentry
Sentry.init()

// Custom tracking
window.addEventListener('error', (err) => {
  console.error('An error occurred:', err)
})
```


## Server-side

You can use the <Link href="/onError">`+onError()`</Link> hook to access server-side errors:

```ts
// pages/+onError.ts
// Environment: server

import type { Config } from 'vike/types'

export const onError: Config['onError'] = (error) => {
  if (!import.meta.env.PROD) return // only track errors in production

  /* Vike already calls console.error() so the following line isn't needed.
  console.error(error)
  */

  // Install error tracker
  myErrorTracker.intercept(error)
}
```

This enables you to install error trackers such as [Sentry](https://sentry.io/), [Bugsnag](https://www.bugsnag.com/), or [Rollbar](https://rollbar.com/). For example with Sentry:

```ts
// pages/+onError.ts
// Environment: server

import * as Sentry from '@sentry/node';
import type { Config } from 'vike/types'

export const onError: Config['onError'] = (error) => {
  Sentry.captureException(error)
}
```


## See also

 - <Link href="/onError" />
 - <Link href="/client" />
 - <Link href="/error-page" />
 - <Link href="/debug#verbose-errors" />
 - [Vite > Built-in Constants > `import.meta.env.PROD`](https://vite.dev/guide/env-and-mode.html#built-in-constants)
